
/*
ATTENTION - THIS FILE CONTAINS THIRD PARTY OPEN SOURCE CODE: PBRT
IT IS CLEARED ONLY FOR LIMITED USE BY MICROSOFT RESEARCH FOR THE
TIME-OF-FLIGHT RAYTRACER PRODUCT. DO NOT USE OR SHARE THIS CODE
WITHOUT APPROVAL PURSUANT TO THE MICROSOFT OPEN SOURCE SOFTWARE
APPROVAL POLICY.

pbrt source code Copyright(c) 1998-2012 Matt Pharr and Greg Humphreys.

This file is part of pbrt.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

#if defined(_MSC_VER)
#pragma once
#endif

#ifndef PBRT_RENDERERS_TOF_H
#define PBRT_RENDERERS_TOF_H


#include "pbrt.h"
#include "renderer.h"
#include "parallel.h"
#include "sampler.h"
#include "renderers/metropolis.h"


struct ToFSample : public MLTSample {
	ToFSample(int maxLength);
	ToFSample(MLTSample &orig);

	vector<HistogramSample> histogram;
};


struct ToFRenderData : public MLTRenderData {
	ToFRenderData(MemoryArena &arena, RNG &rng, PathVertex *cameraPath = NULL, 
	PathVertex* lightPath = NULL, float* cameraPathDistances = NULL,
	float* lightPathDistances = NULL, int cameraPathLength = 0, 
	int lightPathLength = 0);

	float *cameraPathDistances, *lightPathDistances;
};


class ToFRenderer : public MetropolisRenderer {
public:
	ToFRenderer(int perPixelSamples, int nBootstrap,
		float largeStepProbability, int maxConsecutiveRejects, int maxDepth,
		Camera *camera, bool bidirectional, bool groundtruth);

	void Render(const Scene *scene);

private:
	void LaunchDirectTasks(ToFRenderData &data);
	void LaunchMLT(ToFRenderData &data);
	void LaunchMLTTasks(ToFRenderData &data, ToFSample &initialSample, float b);

	Spectrum PathL(ToFRenderData &data, ToFSample &sample) const;
	Spectrum Lpath(ToFRenderData &data, ToFSample &sample) const;
	Spectrum Lbidir(ToFRenderData &data, ToFSample &sample) const;

	bool InSampleBounds(ToFSample &sample) const;
	Spectrum DirectLightingVertex(ToFRenderData &data, ToFSample &sample, 
		int pathIdx, const PathVertex &vc, const Point &pc, const Normal &nc, 
		float *distance) const;
	void SaveToFData(ToFSample &sample, Spectrum L, float distance) const;
	Spectrum LightPathL(MLTRenderData &data, MLTSample &sample,
		const Point &pc, const PathVertex &vc, const Normal &nc,
		int *nSpecularVertices, int cameraPathIdx, int lightPathIdx,
		float *distance) const;

	uint32_t GeneratePath(ToFRenderData &data, ToFSample &sample, 
		const RayDifferential &r, const Spectrum &a, bool isLightPath) const;
	bool GeneratePathVertex(ToFRenderData &data, RayDifferential &ray,
		Spectrum &alpha, int vertexIdx, bool isLightPath, PathVertex *v,
		float *distances) const;

	void WriteHeader();

	bool groundTruth;
	
	friend class ToFTask;
};

ToFRenderer *CreateToFRenderer(const ParamSet &params, Camera *camera);


class ToFTask : public MLTTask {
public:
	ToFTask(ProgressReporter &prog, uint32_t pfreq, uint32_t taskNum,
		float dx, float dy, int xx0, int xx1, int yy0, int yy1, float tt0,
		float tt1, float bb, const ToFSample &is, ToFRenderer *renderer);
	void Run();

private:
	void Init(ToFRenderData &data, ToFSample *samples, Spectrum *L, float *I);
	void SplatSample(float acceptProb, ToFSample *samples, Spectrum *L, float *I);

	const ToFSample &initialSample;
	ToFRenderer *renderer;
};

class ToFDirectTask : public SamplerRendererTask {
public:
	ToFDirectTask(const Scene *scene, Renderer *renderer, Camera *camera,
		ProgressReporter &progress, Sampler *sampler, Sample *sample,
		int taskNum, int taskCount);
	void Run();

private:
	HistogramSample ComputeSample(MemoryArena &arena, RNG &rng, int idx);
	void ValidateL(Spectrum &Ls, float LsAvg);

	Sampler *sampler;
	Sample *samples;
	RayDifferential *rays;
	Spectrum *Ls;
	Spectrum *Ts;
	Intersection *isects;
};

#endif // PBRT_RENDERERS_TOF_H